#!/usr/bin/python

#-*- coding:utf-8 -*-

__author__ = "Carlan Calazans <carlan@gmail.com>"
__version__ = "0.1"
__date__ = "Date: 4/3/2007"
__copyright__ = "Copyright (c) 2007 Carlan Calazans"
__license__ = "GPL"

import sys
sys.path.append('/home/carlan/Pessoal/Projetos/Python/ToDo/')
try:
    import pygtk
    pygtk.require('2.0')
except:
    pass
try:
    import gtk
    import gtk.glade
    import gobject
    from model.todo import *
    from model.todoDao import *
except:
    sys.exit(1)

class appToDo:
    def __init__(self):
        """ """
        #Get top window widget
        self.xml = gtk.glade.XML('glade/todo.glade', 'FormToDo')
        #Get the Main Window, and connect the 'destroy' event
        self.window = self.xml.get_widget('FormToDo')
        if (self.window):
            self.window.connect('destroy', gtk.main_quit)

        #Widgets
        #TreeView
        self.table = self.xml.get_widget('table')
        #TreeView - Column names
        self.strColumnId = 'Id'
        self.strColumnDescricao = 'Descricao'
        self.strColumnComentario = 'Comentario'
        self.strColumnData = 'Data'
        self.strColumnImportancia = 'Importancia'
        #TreeView - Column Ids
        self.intColumnObject = 0
        self.intColumnId = 1
        self.intColumnDescricao = 2
        self.intColumnComentario = 3
        self.intColumnData = 4
        self.intColumnImportancia = 5
        #TreeView - Add all columns
        self.addToDoListColumn(self.strColumnId,self.intColumnId)
        self.addToDoListColumn(self.strColumnDescricao,self.intColumnDescricao)
        self.addToDoListColumn(self.strColumnComentario,self.intColumnComentario)
        self.addToDoListColumn(self.strColumnData,self.intColumnData)
        self.addToDoListColumn(self.strColumnImportancia,self.intColumnImportancia)
        #TreeView - Create model
        #TreeView - object, int, string, string, int, int
        self.tableStore = gtk.ListStore(gobject.TYPE_PYOBJECT,
                                gobject.TYPE_INT,
                                gobject.TYPE_STRING,
                                gobject.TYPE_STRING,
                                gobject.TYPE_INT,
                                gobject.TYPE_INT)
        #TreeView - Just fill the table
        self.dao = ToDoDao()
        self.dao.initTables()
        self.fillTable()
        #TreeView - Set the model
        self.table.set_model(self.tableStore)

        #StatusBar
        self.statusbar = self.xml.get_widget('statusbar')

        #Autoconnect Signals and Callbacks
        self.xml.signal_autoconnect(self)
        self.window.show_all()

    def addToDoListColumn(self, title, columnId):
        """ Add columns to treeView """
        column = gtk.TreeViewColumn(title, gtk.CellRendererText(), text=columnId)
        column.set_resizable(True)
        column.set_sort_column_id(columnId)
        self.table.append_column(column)  

    def fillTable(self):
        """ Fill the table with data stored in database """
        self.tableStore.clear()
        #self.dao = ToDoDao()
        #self.dao.initTables()
        for toDoItem in self.dao.list():
            #self.tableStore.append([toDoItem, toDoItem.id, toDoItem.descricao, toDoItem.comentario, toDoItem.data, toDoItem.importancia]) 
            self.tableStore.append([toDoItem, toDoItem.id, toDoItem.descricao, toDoItem.comentario, toDoItem.data, toDoItem.importancia]) 

    def showErrorDialog(self, erro):
        formErro = gtk.MessageDialog(type=gtk.MESSAGE_ERROR,message_formt=erro,buttons=gtk.BUTTONS_OK)
        formErro.run()
        formErro.destroy()

    #Callbacks
    def on_MenuAdd(self, widget):
        """ Menu Add """
        formToDoAdd = FormToDoAdd()
        result, toDoItem = formToDoAdd.run()
        if(result == gtk.RESPONSE_OK):
            print 'Button OK clicked'
            #toDoItem.id = int(toDoItem.id)
            toDoItem.data = int(toDoItem.data)
            toDoItem.importancia = int(toDoItem.importancia)
            #self.dao = ToDoDao()
            #self.dao.initTables()
            self.dao.insert(toDoItem)
            self.fillTable()

        print 'on_MenuAdd'

    def on_MenuQuit(self, widget):
        """ Menu Quit """
        gtk.main_quit()

    def on_MenuEdit(self, widget):
        """ Menu Edit """
        print 'on_MenuEdit'
        selection = self.table.get_selection()
        model, iterator = selection.get_selected()
        if (iterator):
            toDoItem = self.tableStore.get_value(iterator, self.intColumnObject)

        formToDoAdd = FormToDoAdd(toDoItem)
        result, toDoItem = formToDoAdd.run()

        if(result == gtk.RESPONSE_OK):
            print 'Button OK clicked'
            #toDoItem.id = int(toDoItem.id)
            toDoItem.data = int(toDoItem.data)
            toDoItem.importancia = int(toDoItem.importancia)
            #self.dao = ToDoDao()
            #self.dao.initTables()
            self.dao.update(toDoItem)
            self.fillTable()

    def on_MenuDelete(self, widget):
        """ Menu Delete """
        print 'on_MenuDelete'
        selection = self.table.get_selection()
        model, iterator = selection.get_selected()
        if (iterator):
            toDoItem = self.tableStore.get_value(iterator, self.intColumnObject)
            self.dao.delete(toDoItem.id)
            self.fillTable()
            
    def on_MenuSobre(self, widget):
        """ Menu Sobre """
        print 'on_MenuSobre'
        formToDoAbout = FormToDoAbout()
        result = formToDoAbout.run()
        if(result == gtk.RESPONSE_OK):
            print 'Button OK clicked'

    def on_Add(self, widget):
        """ Toolbar button add """
        formToDoAdd = FormToDoAdd()
        result, toDoItem = formToDoAdd.run()
        if(result == gtk.RESPONSE_OK):
            print 'Button OK clicked'
            #toDoItem.id = int(toDoItem.id)
            toDoItem.data = int(toDoItem.data)
            toDoItem.importancia = int(toDoItem.importancia)
            #self.dao = ToDoDao()
            #self.dao.initTables()
            self.dao.insert(toDoItem)
            self.fillTable()
            
        print 'on_Add'

    def on_Edit(self, widget):
        """ Toolbar button Edit """
        print 'on_Edit'
        selection = self.table.get_selection()
        model, iterator = selection.get_selected()
        if (iterator):
            toDoItem = self.tableStore.get_value(iterator, self.intColumnObject)

        formToDoAdd = FormToDoAdd(toDoItem)
        result, toDoItem = formToDoAdd.run()

        if(result == gtk.RESPONSE_OK):
            print 'Button OK clicked'
            #toDoItem.id = int(toDoItem.id)
            toDoItem.data = int(toDoItem.data)
            toDoItem.importancia = int(toDoItem.importancia)
            #self.dao = ToDoDao()
            #self.dao.initTables()
            self.dao.update(toDoItem)
            self.fillTable()

    def on_Delete(self, widget):
        """ Toolbar button delete """
        print 'on_Delete'
        selection = self.table.get_selection()
        model, iterator = selection.get_selected()
        if (iterator):
            toDoItem = self.tableStore.get_value(iterator, self.intColumnObject)
            self.dao.delete(toDoItem.id)
            self.fillTable()

#Class to add To Do Item
class FormToDoAdd:
    """ """
    def __init__(self, toDoItem=None):
        """ Constructor """
        self.gladefile= 'glade/todo.glade'
        self.isEdit = False
        if(toDoItem):
            self.toDoItem = toDoItem
            self.isEdit = True
        else:
            self.toDoItem = ToDo()

    def run(self):
        """  """
        #Load from glade
        self.xml = gtk.glade.XML(self.gladefile, 'FormToDoAdd')
        #Get actual dialog
        self.dialog = self.xml.get_widget('FormToDoAdd')
        #Get all entry and set their text
        self.txtDescricao = self.xml.get_widget('txtDescricao')
        self.txtComentario = self.xml.get_widget('txtComentario')
        self.txtData = self.xml.get_widget('txtData')
        self.txtImportancia = self.xml.get_widget('txtImportancia')
        
        if(self.isEdit):
            self.txtDescricao.set_text(self.toDoItem.descricao)
            self.txtComentario.set_text(self.toDoItem.comentario)
            self.txtData.set_text( str(self.toDoItem.data) )
            self.txtImportancia.set_text( str(self.toDoItem.importancia) )
        
        #(...)
        #Run dialog and store the response
        self.result = self.dialog.run()
        #Get the value of the entry fields
        self.toDoItem.descricao = self.txtDescricao.get_text()
        self.toDoItem.comentario = self.txtComentario.get_text()
        self.toDoItem.data = self.txtData.get_text()
        self.toDoItem.importancia = self.txtImportancia.get_text()
        #(...)
        #We r done with the dialog, destroy it
        self.dialog.destroy()
        return self.result,self.toDoItem

#Class about
class FormToDoAbout:
    def __init__(self):
        """ Constructor """
        self.gladefile= 'glade/todo.glade'

    def run(self):
        """ Run method of GTkDialog """
        #Load from glade
        self.xml = gtk.glade.XML(self.gladefile, 'FormToDoAbout')
        #Get actual dialog
        self.dialog = self.xml.get_widget('FormToDoAbout')
        #Run dialog and store the response
        self.result = self.dialog.run()
        #We r done with the dialog, bye
        self.dialog.destroy()
        return self.result 

#Auxiliary Methods
if __name__ == "__main__":
    apptodo = appToDo()
    gtk.main()
